package org.emergency.framework.action.oa;

import java.util.List;

import javax.annotation.Resource;

import org.emergency.framework.action.BaseAction;
import org.emergency.framework.model.TbPermission;
import org.emergency.framework.model.TbRole;
import org.emergency.framework.service.common.IRolePermissionMapping;
import org.emergency.framework.service.oa.ITbPermissonService;
import org.emergency.framework.serviceImpl.common.RolePermissionMappingImpl;
import org.springframework.stereotype.Repository;

import com.opensymphony.oscache.util.StringUtil;

@Repository("tbPermissionAction")
public class TbPermissionAction extends BaseAction {

	/**
	 * 角色
	 */
	private TbRole tbRole;
	/**
	 * 
	 */
	private String message;
	/**
	 * 权限列表
	 */
	List<TbPermission> tbPermissionList;

	/**
	 * 接收的permissionList的string
	 */
	private String permissionIdString;

	/**
	 * 接收到的tbPermission
	 */
	private TbPermission tbPermission;

	/**
	 * 服务
	 */
	@Resource(name = "tbpermissionServiceImpl")
	private ITbPermissonService service;

	public String queryPermissionByRoleId() {
		tbPermissionList = service.queryPermissinByRoleId(tbRole.getTbRoleId());
		return "queryPermissionByRoleId";
	}

	/**
	 * 
	 * @return
	 */
	public String submitRolePermissionList() {
		message = "提交成功";
		permissionIdString = permissionIdString.substring(0, permissionIdString
				.lastIndexOf(","));
		List<String> permissionIdStringList = StringUtil.split(
				permissionIdString, ',');
		try {
			service.submitRolePermissionList(tbRole.getTbRoleId(),
					permissionIdStringList);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message = "提交失败";
		}
		return "submitRolePermissionList";
	}

	/**
	 * 增加
	 * @return
	 */
	public String addPermissionByParentId() {
		if(service.save(tbPermission))
			setMessage("增加成功"); 
		else setMessage("增加失败");
		return "addPermissionByParentId";
	}

	/**
	 * 通过 id进行修改
	 * 
	 * @return
	 */
	public String modifyPermissionById(){
			if(service.update(tbPermission))
				setMessage("修改成功");
			else setMessage("修改失败");
		return "modifyPermissionById";
	}

	public TbRole getTbRole() {
		return tbRole;
	}

	public void setTbRole(TbRole tbRole) {
		this.tbRole = tbRole;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<TbPermission> getTbPermissionList() {
		return tbPermissionList;
	}

	public void setTbPermissionList(List<TbPermission> tbPermissionList) {
		this.tbPermissionList = tbPermissionList;
	}

	public ITbPermissonService getService() {
		return service;
	}

	public void setService(ITbPermissonService service) {
		this.service = service;
	}

	public String getPermissionIdString() {
		return permissionIdString;
	}

	public void setPermissionIdString(String permissionIdString) {
		this.permissionIdString = permissionIdString;
	}

	public TbPermission getTbPermission() {
		return tbPermission;
	}

	public void setTbPermission(TbPermission tbPermission) {
		this.tbPermission = tbPermission;
	}

}
